package com.jp.knowledge.my.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.activity.BaseFragment;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.adapter.OrderDetailScheduleAdapter;
import com.jp.knowledge.my.model.OrderDetailScheduleModel;
import com.jp.knowledge.util.ResultSubscriber;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/5/5.
 */

public class OrderDetailScheduleFragment extends BaseFragment implements CanRefreshLayout.OnRefreshListener, ResultSubscriber.OnResultListener {

    private static int GET_ORDER_SCHEDULE_LIST_CODE = 0x01;

    public static OrderDetailScheduleFragment newInstance(String orderId){
        OrderDetailScheduleFragment orderDetailScheduleFragment = new OrderDetailScheduleFragment();
        Bundle bundle = new Bundle();
        bundle.putString("orderId", orderId);
        orderDetailScheduleFragment.setArguments(bundle);
        return orderDetailScheduleFragment;
    }

    private String orderId;
    private Context mContext;

    private View contentView;
    private CanRefreshLayout refreshLayout;
    private RecyclerView recyclerView;

    private List<OrderDetailScheduleModel> orderDetailScheduleModels;
    private OrderDetailScheduleAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
        orderId = getArguments().getString("orderId");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(contentView == null){
            contentView = View.inflate(mContext, R.layout.refresh_recycleview, null);

            refreshLayout = (CanRefreshLayout) contentView.findViewById(R.id.refresh);
            recyclerView = (RecyclerView) contentView.findViewById(R.id.can_content_view);

            refreshLayout.setLoadMoreEnabled(false);
            refreshLayout.setOnRefreshListener(this);

            recyclerView.setPadding(recyclerView.getPaddingLeft(),
                    (int)mContext.getResources().getDimension(R.dimen.space_big),
                    recyclerView.getPaddingRight(),
                    (int)mContext.getResources().getDimension(R.dimen.space_big));
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            orderDetailScheduleModels = new ArrayList<>();
            adapter = new OrderDetailScheduleAdapter(mContext, orderDetailScheduleModels);
            recyclerView.setAdapter(adapter);

            refreshLayout.autoRefresh();
        }
        return contentView;
    }

    @Override
    public void onRefresh() {
        JsonObject param = new JsonObject();
        param.addProperty("orderId", orderId);
        HttpHelper.getInstance(mContext).getOrderScheduleList(param, GET_ORDER_SCHEDULE_LIST_CODE, this);
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {
        if(requestType == GET_ORDER_SCHEDULE_LIST_CODE)
            refreshLayout.refreshComplete();
    }
    @Override
    public void onError(int requestType) {
        onCompleted(requestType);
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            ToasUtil.toast(mContext, data.getMessage());
            onError(requestType);
        } else if(requestType == GET_ORDER_SCHEDULE_LIST_CODE){
            orderDetailScheduleModels = data.getList(OrderDetailScheduleModel.class);
            adapter.updateAdapter(orderDetailScheduleModels);
        }
    }
}
